<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Construct a new file object.</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.splfileobject.html">SplFileObject</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="splfileobject.current.html">SplFileObject::current</a></div>
 <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="splfileobject.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SplFileObject::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0)</p><p class="refpurpose"><span class="refname">SplFileObject::__construct</span> &mdash; <span class="dc-title">Construct a new file object.</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-splfileobject.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SplFileObject::__construct</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$open_mode</code><span class="initializer"> = &quot;r&quot;</span></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
  ]]] )</div>

  <p class="para rdfs-comment">
   Construct a new file object.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-splfileobject.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">filename</code></em></dt>

     <dd>

      <p class="para">
       The file to read.
      </p>
      <div class="tip"><strong class="tip">Tip</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
     </dd>

    
    
     <dt>
<em><code class="parameter">open_mode</code></em></dt>

     <dd>

      <p class="para">
       The mode in which to open the file. See <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for a list of allowed modes.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">use_include_path</code></em></dt>

     <dd>

      <p class="para">
       Whether to search in the <a href="ini.core.html#ini.include-path" class="link">include_path</a> for <em><code class="parameter">filename</code></em>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">context</code></em></dt>

     <dd>

      <p class="para">
       A valid context resource created with <span class="function"><a href="function.stream-context-create.html" class="function">stream_context_create()</a></span>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-splfileobject.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-splfileobject.construct-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   Throws a <a href="class.runtimeexception.html" class="classname">RuntimeException</a> if the <em><code class="parameter">filename</code></em> cannot be opened.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-splfileobject.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3852">
    <p><strong>Example #1 <span class="methodname"><strong>SplFileObject::__construct()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     This example opens the current file and iterates over its contents line by line.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplFileObject</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$line_num&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Line&nbsp;</span><span style="color: #0000BB">$line_num</span><span style="color: #DD0000">&nbsp;is&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Line 0 is &lt;?php
Line 1 is $file = new SplFileObject(__FILE__);
Line 2 is foreach ($file as $line_num =&gt; $line) {
Line 3 is     echo &quot;Line $line_num is $line&quot;;
Line 4 is }
Line 5 is ?&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-splfileobject.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="splfileinfo.openfile.html" class="methodname" rel="rdfs-seeAlso">SplFileInfo::openFile()</a> - Gets an SplFileObject object for the file</span></li>
    <li class="member"><span class="function"><a href="function.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.splfileobject.html">SplFileObject</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="splfileobject.current.html">SplFileObject::current</a></div>
 <div class="up"><a href="class.splfileobject.html">SplFileObject</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
